package habitcoach.core.habit;

import habitcoach.core.Environmental;
import habitcoach.core.reminder.DateChangeEventListener;
import habitcoach.core.task.Task;
import habitcoach.core.task.TaskFactory;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * Implementation of Habit
 * 
 * @author Coddy created on 2010-4-24
 * 
 */
class HabitImpl implements Habit, DateChangeEventListener, Serializable
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 4426928105975099456L;
	private static Environmental environmental;
	private int ID;
	private String title;
	private String category;
	private String question;
	private Date startDate;
	private HabitStatus status;
	private List<Task> taskList;
	private int index; // index of current task

	public HabitImpl(Environmental env, String title, String category,
			String question)
	{
		HabitImpl.environmental = env;
		this.startDate = environmental.getDate();
		this.title = title;
		this.category = category;
		this.question = question;
		this.taskList = createDailyTaskList();
		this.status = Habit.DEFAULT_HABIT_STATUS;
		this.index = 0;
		this.ID = 0;
	}

	public HabitImpl()
	{

	}

	private List<Task> createDailyTaskList()
	{

		TaskFactory taskFactory = TaskFactory.getInstance();

		return taskFactory.createTaskList();
	}

	@Override
	public void onDateChanged()
	{
		// TODO actions when a new day comes
		index++;
	}

	public void appendTask(Task task)
	{
		taskList.add(task);
	}

	public String getQuestion()
	{
		return question;
	}

	public Date getStartDate()
	{
		return startDate;
	}

	public HabitStatus getStatus()
	{
		return status;
	}

	public void setStatus(HabitStatus status)
	{
		this.status = status;
	}

	public static Environmental getEnvironmental()
	{
		return environmental;
	}

	public static void setEnvironmental(Environmental environmental)
	{
		HabitImpl.environmental = environmental;
	}

	void setQuestion(String question)
	{
		this.question = question;
	}

	void setStartDate(Date startDate)
	{
		this.startDate = startDate;
	}

	void setTaskList(List<Task> taskList)
	{
		this.taskList = taskList;
	}

	List<Task> getTaskList()
	{
		return taskList;
	}

	public Task getTask(int index)
	{
		if (index < 0 || index >= taskList.size())
		{
			throw new IndexOutOfBoundsException();
		}
		return taskList.get(index);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see habitcoach.core.habit.Habit#getIndex()
	 */
	@Override
	public int getIndex()
	{
		return index;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see habitcoach.core.habit.Habit#getTaskCount()
	 */
	@Override
	public int getTaskCount()
	{
		return taskList.size();
	}

	public String getTitle()
	{
		return title;
	}

	protected void setTitle(String title)
	{
		this.title = title;
	}

	public String getCategory()
	{
		return category;
	}

	protected void setCategory(String category)
	{
		this.category = category;
	}

	@Override
	public int getID()
	{
		return ID;
	}

	void setID(int iD)
	{
		ID = iD;
	}
}
